package com.inspinia.base.heartbeat.util;

import java.util.Observable;
import java.util.Observer;

/**
 * 浏览器心跳的观察者
 * Created by veblen on 2017/1/1.
 */
public abstract class BrowserHeartbeatObserver implements Observer {


    /**
     * 失去心跳事件
     *
     * @param userId 用户id
     */
    public abstract void onLostHeartbeat(Long userId);

    /**
     * 第一次获取到心跳事件
     *
     * @param userId 用户id
     */
    public abstract void onGetFirstHeartbeat(Long userId);

    @Override
    public void update(Observable o, Object arg) {
        BrowserHeartbeatUserId heartbeatUserId = (BrowserHeartbeatUserId) arg;

        Long lostHbUserId = heartbeatUserId.getLostHbUserId();
        if (lostHbUserId != null) {
//            System.out.println("失去心跳:" + lostHbUserId);
            onLostHeartbeat(lostHbUserId);
        }
        Long firstGetHbUserId = heartbeatUserId.getFirstGetHbUserId();
        if (firstGetHbUserId != null) {
//            System.out.println("第一次获取到心跳" + firstGetHbUserId);
            onGetFirstHeartbeat(firstGetHbUserId);
        }
    }
}
